import { Message } from 'element-ui';

// type AudioType = 'scanBarSuccess' | 'scanBarError' | 'noSame' | 'checkSuccess' | 'scanGoodsSu' | 'scanSendError' | ''
// const audioMap = {
//   'scanBarSuccess': require('@/assets/audio/checkIn.mp3'),
//   'scanBarError': require('@/assets/audio/checkOut.mp3'),
//   'noSame': require('@/assets/audio/CheckSendSu.mp3'),
//   'checkSuccess': require('@/assets/audio/checkSuccess.mp3'),
//   'scanGoodsSu': require('@/assets/audio/ScanGoodsSu.mp3'),
//   'scanSendError': require('@/assets/audio/ScanSendError.mp3')
// }

class AudioPlayPlugin {
    // private audioType:AudioType // 声音类别
    private timer: any; // 定时器
    private playFlag: boolean; // 是否需要暂停
    public playAuth: boolean; // 音效播放的权限
    public audioDom: HTMLAudioElement;
    constructor(audioDom: HTMLAudioElement, playAuth: boolean = true) {
        this.playAuth = playAuth;
        this.audioDom = audioDom;
        this.playFlag = false;
        this.timer = null;
    }
    playNumberAudio(num: number) {
        // 这个属性现在暂时浏览器版本不支持
        // if ('speechSynthesis' in window) {
        //   console.log('有原生播报')
        //   // 优先找原生播报
        //   // const speek = window.speechSynthesis
        //   const msg = new SpeechSynthesisUtterance(`扫描${num}件`)
        //   msg.lang = 'zh-CN'
        //   msg.text = `扫描${num}件`
        //   msg.volume = 1 // 音量 [0, 1]
        //   msg.rate = 2 // 语速
        //   window.speechSynthesis.speak(msg)
        //   // const msg = new SpeechSynthesisUtterance('ANIPLEX')
        //   // window.speechSynthesis.speak(msg)
        //   return
        // }
        const BaiduAPI: string = 'https://tsn.baidu.com/text2audio'; // 百度播报API
        const audioParam: any = {
            cuid: 'baike',
            lan: 'ZH',
            ctp: 5,
            pdt: 301,
            vol: 4,
            rate: 32,
            per: 0, // 人物类型 0,1,2,3,4,5
            tex: `扫描${num}件`
        };
        const param: string = Object.keys(audioParam)
            .map((key) => {
                return `${key}=${audioParam[key]}`;
            })
            .join('&');
        const url: string = `${BaiduAPI}?${param}`;
        const audio = new Audio(url);
        audio.play();
    }

    playAudio() {
        // const audio = this.$refs.audio as any
        //   const audioSrc = audioMap[audioType]
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = null;
        }
        try {
            if (this.audioDom !== null) {
                if (this.playFlag) {
                    // 暂停
                    this.audioDom.pause();
                    this.playFlag = false;
                } else {
                    this.audioDom.currentTime = 0; // 控制播放时间
                    const playPromise = this.audioDom.play();
                    if (playPromise) {
                        playPromise
                            .then(() => {
                                // 音频加载成功
                                this.timer = setTimeout(() => {}, this.audioDom.duration * 1000); // 音频的时长
                            })
                            .catch(() => {
                                // 失败后继续调用播放
                                this.playFlag = true;
                                this.playAudio();
                            });
                    }
                }
            }
        } catch (e) {
            Message({
                type: 'error',
                message: '播放异常',
                duration: 5000
            });
            console.error(e);
        }
    }
}
export default AudioPlayPlugin;
